//
//  MyFMViewController.m
//  DolphinMusic
//
//  Created by liuhaiyang on 15/4/2.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "MyFMViewController.h"
#import "fmCollectionTableViewCell.h"
#import "AlbumViewController.h"


@interface MyFMViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *albumArr;
@property (nonatomic, retain)UIScrollView *scrollView;
@property (nonatomic, retain)UILabel *nullLabel;



@end

@implementation MyFMViewController

- (void)dealloc
{
    [_nullLabel release];
    [_tableView release];
    [_albumArr release];
    [_scrollView release];
    [super dealloc];
}


- (void)receieveNotification:(NSNotification *)noti
{
    DataBaseHandler *dbHandler = [DataBaseHandler shareInstance];
    self.albumArr = [dbHandler selectAllInFMalbumCollection];
    
    NSInteger albumCount = self.albumArr.count;
    for (NSInteger j = 0; j < albumCount / 2; j++) {
        [self.albumArr exchangeObjectAtIndex:j withObjectAtIndex:albumCount - 1 - j];
    }
    if (albumCount == 0) {
        self.nullLabel.hidden = NO;
    }
    else {
        self.nullLabel.hidden = YES;
    }
    
    self.tableView.frame = CGRectMake(0, 0, VIEW_WIDTH - 10, self.albumArr.count * 55);
    self.scrollView.contentSize = CGSizeMake(0, self.albumArr.count * 55);
    [self.tableView reloadData];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(receieveNotification:) name:@"fmalbumCollectionReload" object:nil];
    
    UIWindow *window = [[[UIApplication sharedApplication] windows] firstObject];
    PlayView *playView = (PlayView*)[window viewWithTag:10001];
    playView.target = self.navigationController;

    
    self.view.backgroundColor = [UIColor colorFromHexCode:@"##f2eada"];
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, 20)];
    view1.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view1];
    [view1 release];
    
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 20, VIEW_WIDTH, 44)];
    view2.backgroundColor = [UIColor colorFromHexCode:@"#33a3dc"];
    [self.view addSubview:view2];
    [view2 release];
    
    
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeSystem];
    leftButton.frame = CGRectMake(0, 20, 50, 44);
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 12, 20, 20)];
    imageView.image = [UIImage imageNamed:@"backButton.png"];
    [leftButton addSubview:imageView];
    [imageView release];
    
    [leftButton addTarget:self action:@selector(leftButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:leftButton];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 27, 100, 30)];
    titleLabel.text = @"我的电台";
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.font = [UIFont systemFontOfSize:15];
    [self.view addSubview:titleLabel];
    [titleLabel release];
    
    
    
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 80, VIEW_WIDTH - 10, VIEW_HEIGHT - 80 - 70.0 / 667 *VIEW_HEIGHT)];
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.contentSize = CGSizeMake(0, 55 * self.albumArr.count);
    [self.view addSubview:self.scrollView];
    [_scrollView release];
    
    self.nullLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 100, VIEW_WIDTH - 160, 20)];
    self.nullLabel.text = @"暂无收藏电台";
    self.nullLabel.font = [UIFont systemFontOfSize:14];
    self.nullLabel.textColor = [UIColor grayColor];
    self.nullLabel.textAlignment = NSTextAlignmentCenter;
    [self.scrollView addSubview:self.nullLabel];
    self.nullLabel.hidden = YES;
    [_nullLabel release];
    
    
    
    
    
    DataBaseHandler *dbHandler = [DataBaseHandler shareInstance];
    self.albumArr = [dbHandler selectAllInFMalbumCollection];
    NSInteger albumCount = self.albumArr.count;
    for (NSInteger j = 0; j < albumCount / 2; j++) {
        [self.albumArr exchangeObjectAtIndex:j withObjectAtIndex:albumCount - 1 - j];
    }
    
    if (albumCount == 0) {
        self.nullLabel.hidden = NO;
    }
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH - 10, 55 * self.albumArr.count) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.rowHeight = 55;
    [self.tableView registerClass:[fmCollectionTableViewCell class] forCellReuseIdentifier:@"myfmcollectionreuse"];
    [self.scrollView addSubview:self.tableView];
    [_tableView release];

    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.albumArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    fmCollectionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myfmcollectionreuse" forIndexPath:indexPath];
    cell.album = [self.albumArr objectAtIndex:indexPath.row];
    cell.backgroundColor = [UIColor clearColor];
    return  cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    AlbumViewController *albumVC = [[AlbumViewController alloc] init];
    FMAlbum *album = [self.albumArr objectAtIndex:indexPath.row];
    albumVC.title11 = album.title;
    albumVC.albumID = album.albumID;
    albumVC.address = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/album/track/%@/true/1/15", album.albumID];
    [self.navigationController pushViewController:albumVC animated:YES];
    [albumVC release];
}

- (void)leftButtonAction:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    UIButton *button = (UIButton *) [window viewWithTag:1000];
    button.hidden = YES;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
